import java.util.*;
import java.util.concurrent.Callable;

public final class MCsimulationTask implements Callable<Double> {

	private String callPutFlag;
	private double s;
	private double x;
	private double t; 
	private double r;
	private double b;
	private double v;
	private int nSteps;
	
	public MCsimulationTask()
	{

	}

	public MCsimulationTask(String callPutFlag, double s, double x, double t, double r, 
			double b, double v, int nSteps)
	{
		this.callPutFlag = callPutFlag;
		this.s = s;
		this.x = x;
		this.t = t; 
		this.r = r;
		this.b = b;
		this.v = v;
		this.nSteps = nSteps;
	}

	@Override
	public Double call() throws Exception {
		// TODO Auto-generated method stub
		// 1 task is 1 Monte Carlo simulation
		double dt;
		double st;
		double result = 0;
		double drift;
		double vSqrDt;
		int j;
		int z = 0;

		Random random = new Random();

		dt = t/nSteps;
		drift = (b - (v*v) / 2)*dt;
		vSqrDt = v*java.lang.Math.sqrt(dt);

		if(callPutFlag.equals("c"))
			z = 1;

		else if (callPutFlag.equals("p"))
			z = -1;

		st = s;

		for(j=1; j<=nSteps; j++)
		{
			st = st * java.lang.Math.exp( drift + vSqrDt * random.nextGaussian() );
		}

		result = java.lang.Math.max(z * (st - x), 0);

		return result;
	}
		
	public String getCallPutFlag() {
		return this.callPutFlag;
	}
	
	public Double getS() {
		return this.s;
	}	

	public Double getX() {
		return this.x;
	}
	
	public Double getT() {
		return this.t; 
	}
	
	public Double getR() {
		return this.r;
	}
	
	public Double getB() {
		return this.b;
	}
	
	public Double getV() {
		return this.v;
	}
	
	public int getNSteps() {
		return this.nSteps;
	}

}